package com.design3.graduatethesissys.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.server.PathParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.design3.graduatethesissys.common.utils.FileUtil;
import com.design3.graduatethesissys.common.utils.GetUserName;
import com.design3.graduatethesissys.domain.Regist;
import com.design3.graduatethesissys.service.impl.SubjectServiceImpl;
import com.design3.graduatethesissys.service.impl.UserServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/** 
* @author 作者 罗穆健: 
* @version 创建时间：2020年1月5日 下午8:22:17 
* 类说明 
*/
@Controller
@RequestMapping("/Admin")
public class Admin {
	
	@Autowired
	UserServiceImpl userservice;
	
	@Autowired
	SubjectServiceImpl subjectService;
	
	@RequestMapping(value = "")
	public String index() {
		return "Admin";
	}
	
	/**
	 * 如果注册老师需要返回一个标志标明注册的是老师，学生同理，注册需要发送老师或者学生表的内容给后端
	 * @return 注册成功返回success，失败返回false
	 */
	@ResponseBody
	@RequestMapping(value="/regist")
	public String regist(Regist regist) {
		if("ROLE_STUDENT".equals(regist.getRole())) {
			regist.setStu_id(regist.getUsername());
			regist.setStu_mail(regist.getMail());
			regist.setStu_name(regist.getName());
			regist.setStu_phone(regist.getPhone());
			regist.setStu_status("st01");
			regist.setStu_sex(regist.getSex());
		} else if("ROLE_TEACHER".equals(regist.getRole())) {
			regist.setTeh_id(regist.getUsername());
			regist.setTeh_email(regist.getMail());
			regist.setTeh_name(regist.getName());
			regist.setTeh_phone(regist.getPhone());
			regist.setTeh_sex(regist.getSex());
		}
		return userservice.registUser(regist);
	}
	/**
	 * 审核题目
	 * @return 成功返回success，失败返回error
	 */
	@ResponseBody
	@RequestMapping(value = "/approve")
	public String approveSubject(@RequestParam("sbj_id") String sbj_id,@RequestParam("is_pass")int is_pass) {
		return userservice.approveSubject(sbj_id,is_pass);
	}

	/**
	 * @author 王新宇
	 * @return 返回未审核课题页面
	 */
	@RequestMapping(value = "/uncheck")
	public String uncheckPage(ModelMap data) {
		int amout = subjectService.getUncheckSubject();
		data.addAttribute("amout",amout);
		return "Admin/uncheck";
	}

	/**
	 * @author 王新宇
	 * @return 返回已通过课题页面
	 */
	@RequestMapping(value = "/pass")
	public String passPage(ModelMap data) {
		int amout = subjectService.getCheckSubject();
		data.addAttribute("amout",amout);
		return "Admin/pass";
	}

	/**
	 * @author 王新宇
	 * @return 返回未通过课题页面
	 */
	@RequestMapping(value = "/refuse")
	public String refusePage(ModelMap data) {
		int amout = subjectService.getRefuseSubject();
		data.addAttribute("amout",amout);
		return "Admin/refuse";
	}
	
	/**
	 * 
	 * @param curPage
	 * @param limit
	 * @return 未审核的数据
	 */
	@RequestMapping(value = "/getchecksub1")
	@ResponseBody
	public List<Map<String, Object>> getCheckSub1(@RequestParam("curPage") int curPage, @RequestParam("limit") int limit) {
		PageHelper.startPage(curPage, limit);
		List<Map<String, Object>> subjectList = subjectService.uncheckSubjectSearch();
		// 得到分页的结果对象
		PageInfo<Map<String, Object>> personPageInfo = new PageInfo<>(subjectList);
		// 得到分页中的person条目对象
		List<Map<String, Object>> pageList = personPageInfo.getList();
		return pageList;
	}
	
	/**
	 * 
	 * @param curPage
	 * @param limit
	 * @return 已通过
	 */
	@RequestMapping(value = "/getchecksub2")
	@ResponseBody
	public List<Map<String, Object>> getCheckSub2(@RequestParam("curPage") int curPage, @RequestParam("limit") int limit) {
		PageHelper.startPage(curPage, limit);
		List<Map<String, Object>> subjectList = subjectService.checkSubjectSearch();
		// 得到分页的结果对象
		PageInfo<Map<String, Object>> personPageInfo = new PageInfo<>(subjectList);
		// 得到分页中的person条目对象
		List<Map<String, Object>> pageList = personPageInfo.getList();
		System.out.println(JSON.toJSONString(pageList));
		return pageList;
	}
	
	/**
	 * 
	 * @param curPage
	 * @param limit
	 * @return 未通过
	 */
	@RequestMapping(value = "/getchecksub3")
	@ResponseBody
	public List<Map<String, Object>> getCheckSub3(@RequestParam("curPage") int curPage, @RequestParam("limit") int limit) {
		PageHelper.startPage(curPage, limit);
		List<Map<String, Object>> subjectList = subjectService.refuseSubjectSearch();
		// 得到分页的结果对象
		PageInfo<Map<String, Object>> personPageInfo = new PageInfo<>(subjectList);
		// 得到分页中的person条目对象
		List<Map<String, Object>> pageList = personPageInfo.getList();
		return pageList;
	}
	
	@RequestMapping(value = "/getsubdetail")
	@ResponseBody
	public Map<String, Object> getSubDetail(@RequestParam("sub_id") String sub_id){
		return subjectService.getSubDetail(sub_id);
	}
	
	/**
	 * 未审核
	 * @param teachername
	 * @param teacherid
	 * @param subname
	 * @return
	 */
	@RequestMapping(value = "/query1")
	@ResponseBody
	public List<Map<String, Object>> querySubject1(@RequestParam(value = "teachername",required = false) String teachername,
			@RequestParam(value = "teacherid",required = false) String teacherid,
			@RequestParam(value = "subname",required = false) String subname){
				return subjectService.querySubject1(teachername,teacherid,subname);

	}
	/**
	 * 已通过
	 * @param teachername
	 * @param teacherid
	 * @param subname
	 * @return
	 */
	@RequestMapping(value = "/query2")
	@ResponseBody
	public List<Map<String, Object>> querySubject2(@RequestParam(value = "teachername",required = false) String teachername,
			@RequestParam(value = "teacherid",required = false) String teacherid,
			@RequestParam(value = "subname",required = false) String subname){
				return subjectService.querySubject2(teachername,teacherid,subname);

	}
	/**
	 * 未通过
	 * @param teachername
	 * @param teacherid
	 * @param subname
	 * @return
	 */
	@RequestMapping(value = "/query3")
	@ResponseBody
	public List<Map<String, Object>> querySubject3(@RequestParam(value = "teachername",required = false) String teachername,
			@RequestParam(value = "teacherid",required = false) String teacherid,
			@RequestParam(value = "subname",required = false) String subname){
				return subjectService.querySubject3(teachername,teacherid,subname);
	}
	
	/**
	 * 下载报告接口
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/download")
	public String downloadWord(HttpServletResponse response,@RequestParam("sub_id") String subId,@RequestParam("teacherid") String teacherId){
		return FileUtil.download(response, teacherId, subId);
	}

	/**
	 * 返回教师注册页面
	 * @return
	 */
	@RequestMapping(value = "/tearegist")
	public String teaRegist(){
		return "Admin/teaaccount";
	}

	/**
	 * 返回学生注册页面
	 * @return
	 */
	@RequestMapping(value = "/sturegist")
	public String stuRegist(){
		return "Admin/stuaccount";
	}
}
 